<?php
ob_start();
header('Content-type: text/html;charset=utf-8');
/*
	此文件主要功能如下：
		1.在数据库中添加新记录
		2.返回新加记录信息。JSON格式
		3.创建上传目录
	此文件主要在数据库中添加新的记录并返回文件信息
		如果存在则在数据库中添加一条相同记录。返回添加的信息
		如果不存在，则向数据库中添加一条记录。并返回此记录ID
	控件每次计算完文件MD5时都将向信息上传到此文件中
	@更新记录：
		2017-05-29 增加块路径生成逻辑
		2017-08-17 取消对redis的引用
		2014-08-12 完成逻辑。
*/
require '../../vendor/autoload.php';

use database\DBFile;
use utils\PathTool;
use utils\WebBase;
use sql\SqlExec;
use filemgr\BizFolder;

function rename_file($id,$pathRel,$name)
{
    //新的相对路径
    $pathRel = dirname($pathRel);
    $pathRel = PathTool::combin($pathRel, $name);
    
    //根据相对路径(新)=>查找同名文件
    $se = new SqlExec();
    $s = $se->read("up7_files", "f_pathRel", array("f_pathRel"=>$pathRel,"f_deleted"=>false));
    
    $exist = $s != null;
    
    if (!$exist)
	{   
	    //更新名称
	    $se->update2("up7_files",
	        array("f_nameLoc"=>$name,
	            "f_nameSvr"=>$name,
	            "f_pathRel"=>$pathRel
	        ),
	        array("f_id"=>$id));
	    
	    $ret = array("state"=>true,"pathRel"=>$pathRel);
	    echo( json_encode($ret,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE));
	}
	else
	{
	    $res = array("state"=>false,"msg"=>"存在同名文件","code"=>"102");
	    echo( json_encode($res,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE) );
	}
}

function rename_folder($id,$pathRel,$name)
{
    $parentDir = dirname($pathRel);
    $parentDir = str_replace("\\", "/", $parentDir);
    
    //根目录
    $isRootDir = $parentDir == "/";
    
    if($isRootDir)
    {
        renameRootDir($id,$pathRel,$name);
    }
    else
    {
        renameChildDir($id,$pathRel,$name);
    }
}

function renameRootDir($id,$pathRel,$name)
{
    $pathRelOld = $pathRel;
    //新的相对路径
    $pathRelNew = "/" . $name;
    
    $se = new SqlExec();
    $s = $se->read("up7_files", "f_pathRel", array("f_pathRel"=>$pathRelNew,"f_deleted"=>false));
    
    $exist = $s != null;
    
    if (!$exist)
    {
        //更新文件夹相对路径，名称
        $se->update2("up7_files",
            array("f_nameLoc"=>$name,
                "f_nameSvr"=>$name,
                "f_pathRel"=>$pathRelNew
            ),
            array("f_id"=>$id));
        
        DBFile::updatePathRel($pathRelOld,$pathRelNew);
        BizFolder::updatePathRel($pathRelOld,$pathRelNew);      
        
        $ret = array("state"=>true,"pathRel"=>$pathRelNew);
        echo( json_encode($ret,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE));
    }
    else
    {
        $res = array("state"=>false,"msg"=>"存在同名目录","code"=>"102");
        echo( json_encode($res,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE) );
    }
}

function renameChildDir($id,$pathRel,$name)
{
    $pathRelOld = $pathRel;    
    $pos = strrpos($pathRel, "/");
    $pathRel = substr($pathRel, 0,$pos+1);
    $pathRel = $pathRel . $name;
    $pathRelNew = $pathRel;
    
    $se = new SqlExec();
    $s = $se->read("up7_folders", "f_pathRel", array("f_pathRel"=>$pathRelNew,"f_deleted"=>false));
    
    $exist = $s != null;
    
    if (!$exist)
    {
        //更新文件夹相对路径，名称
        $se->update2("up7_folders",
            array("f_nameLoc"=>$name,
                "f_pathRel"=>$pathRelNew
            ),
            array("f_id"=>$id));
        
        DBFile::updatePathRel($pathRelOld,$pathRelNew);
        BizFolder::updatePathRel($pathRelOld,$pathRelNew);
        
        $ret = array("state"=>true,"pathRel"=>$pathRelNew);
        echo( json_encode($ret,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE));
    }
    else
    {
        $res = array("state"=>false,"msg"=>"存在同名目录","code"=>"102");
        echo( json_encode($res,JSON_UNESCAPED_SLASHES| JSON_UNESCAPED_UNICODE) );
    }
}


$wb = new WebBase;
$id  = $wb->queryString("f_id");
$folder  = $wb->reqBool("f_fdTask");
$pathRel = $wb->queryString("f_pathRel");
$nameNew = $wb->queryString("f_nameLoc");
$pathRel = PathTool::url_decode($pathRel);
$nameNew = PathTool::url_decode($nameNew);

if($folder)
{
	rename_folder($id,$pathRel,$nameNew);
}
else
{
	rename_file($id,$pathRel,$nameNew);
}
?>